package com.fangcang.elasticsearch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.util.CollectionUtils;

/**
 * 统计去哪儿推送数据任务
 * @author hecaiqing
 * @date 2018-11-26 15:44:30
 *
 */
public class QunarPushTask implements Runnable {
	
	private Logger logger = Logger.getLogger(QunarPushTask.class);
	
	private List<Map<String, Object>> datas;
	
	private String fromTime;
	
	public QunarPushTask(List<Map<String, Object>> datas,  String fromTime) {
		this.datas = datas;
		this.fromTime = fromTime;
	}

	@Override
	public void run() {
		
		if(CollectionUtils.isEmpty(datas)) {
			logger.info("ElasticSearch中未查询到去哪儿增量推送日志");
		}
		
		Map<String, List<Map<String, Object>>> traceMap = new HashMap<String, List<Map<String, Object>>>();
		
		try {
			//按traceId组装List，将同一个traceId的数据放到一个List对象中
			for(Map<String, Object> map : datas) {
				
//				String traceId = (String)map.get("traceId");
				String merchantCode =  (String)map.get("merchantCode");
				if(merchantCode == null  || "".equals(merchantCode)) {
					continue;
				}
				String apiName =  (String)map.get("bizName");
				
				String key = fromTime + ":" + merchantCode + ":" + apiName;
				if(!traceMap.containsKey(key)) {
					List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
					list.add(map);
					traceMap.put(key, list);
				}else {
					traceMap.get(key).add(map);
				}
			}
			
			List<CostTimeDto> costList = new ArrayList<CostTimeDto>();
			if(!traceMap.isEmpty()) {
				Set<String> keys = traceMap.keySet();
				for(String key : keys) {
					List<Map<String, Object>> list = traceMap.get(key);
					if(CollectionUtils.isEmpty(list)) {
						continue;
					}
					int succSum = 0;
					int errorSum = 0;
					for(Map<String, Object> data : list) {
						//抽取info级别的日志中的costTime耗时
						//商户代码
//						String merchantCode = String.valueOf(data.get("merchantCode"));
						
						//统计各商户推送时间
						if("info".equals(data.get("logType"))) {
//							Object costObj = data.get("costTime");
							//推送耗时
//							long costTime = Long.valueOf(String.valueOf(costObj==null?0l:costObj));
							CostTimeDto cost = new CostTimeDto();
//							cost.setCostTime(costTime);
//							
//							cost.setMerchantCode(merchantCode);
//							cost.setTraceId(key);
//							cost.setStatisticDate(fromTime);
//							cost.setFuncType("QUNAR_PUSH");
//							cost.setChannelCode("qunar");
							costList.add(cost);
							
							succSum++;
						}
						
						if("error".equals(data.get("logType"))) {
							 errorSum++;
						}
						
					}
					
					InvokerRateDto rateDto = new InvokerRateDto();
					rateDto.setTraceId(key);
					rateDto.setSuccCount(succSum);
					rateDto.setFailCount(errorSum);
					rateDto.setTotalCount(succSum+errorSum);
					rateDto.setChannelCode("qunar");
					rateDto.setFuncType("QUNAR_PUSH");
					
					
					System.out.println("商家："+rateDto.getTraceId()+"，总笔数：" + rateDto.getTotalCount()  + "，成功笔数："+rateDto.getSuccCount()+"，失败笔数："+rateDto.getFailCount());
					
					//写库保存统计信息(统计每十分钟耗时区间统计，推送不合格次数商家分布)
					
					
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		//写入数据库表：推送耗时表+推送成功率表
	
		
		
		/**
		 * traceId：traceId
		 * 商户代码：merchantCode
		 * 供应商编码：supplierCode
		 * 渠道编码：channelCode
		 * 统计时间：statisticDate --201811261650
		 * 耗时：costTime    毫秒
		 * 功能类型：funcType--去哪儿变价推送
		 * 
		 */
	}

}
